package ciyuan.ChangeMinecraft;

import ciyuan.Block.Machine.Simple.Anvil.AnvilTileEntity;
import ciyuan.Block.Machine.Simple.SimpleMachineRegister;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.gui.screen.inventory.AnvilScreen;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class ReplaceAnvilToMine {

    @SubscribeEvent
    public static void onGuiOpened(GuiOpenEvent event) {
        if (event.getGui() instanceof AnvilScreen) {
            event.setGui(null);
        }
    }

    @SubscribeEvent
    public static void onClick(PlayerInteractEvent.RightClickBlock event) {
        PlayerEntity player = event.getPlayer();
        if (!player.level.isClientSide) {
            Block block = player.level.getBlockState(event.getHitVec().getBlockPos()).getBlock();
            if (block.equals(Blocks.ANVIL) || block.equals(Blocks.CHIPPED_ANVIL) || block.equals(Blocks.DAMAGED_ANVIL)) {
                player.level.setBlock(event.getHitVec().getBlockPos(), SimpleMachineRegister.ANVIL_BLOCK.get().defaultBlockState(), 0);
                player.level.setBlockEntity(event.getHitVec().getBlockPos(), new AnvilTileEntity());
            }
        }
    }

}
